<template>
  <div class="home-container">
    <!-- 顶部 -->
    <van-nav-bar fixed placeholder>
      <template #left>
        <div class="logo"></div>
      </template>
      <template #right>
        <van-button icon="search" round type="primary" @click="$router.push('/search')">搜索</van-button>
      </template>
    </van-nav-bar>
    <!-- tab栏 -->
    <van-tabs v-model="active" @click="tabClick">
      <van-tab v-for="item in channels" :key="item.id" :title="item.name">
        <!-- 上拉刷新 -->
        <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
          <!-- 上拉 -->
          <van-list
            v-model="loading"
            :finished="finished"
            finished-text="没有更多了"
            @load="onLoad"
          >
            <!-- 列表内容 -->
            <van-cell v-for="item in list" :key="item.art_id" :title="item.title"
            @click="$router.push({path:'/article',query:{id:item.art_id}})"
            >
              <template #label>
                <div>
                  <van-grid  :column-num="item.cover.type">
                    <van-grid-item v-for="(value,index) in item.cover.images" :key="index">
                      <van-image
                        :src="value"
                        lazy-load
                      />
                    </van-grid-item>
                  </van-grid>
                  <div>
                    <span>{{item.aut_name}}</span>&nbsp;
                    <span>{{item.comm_count}}</span>&nbsp;
                    <span>{{item.pubdate | fromNow}}</span>
                  </div>
                </div>
              </template>
            </van-cell>
          </van-list>
        </van-pull-refresh>
      </van-tab>
    </van-tabs>
  </div>
</template>

<script>
// import { now } from 'moment'
import { getChannelList , getArticleList} from './home.js'
export default {
  name: 'home',
  data () {
    return {
      active: 0,
      channels: [], //频道列表
      list: [], //列表数据
      loading: false, //上拉 加载更多动画
      finished: false, //上拉 加载全部数据开关
      isLoading: false, //下拉 刷新动画
      timestamp:null
    }
  },
  async created () {
    let res = await getChannelList()
    console.log(res)
    this.channels = res.data.data.channels
  },
  methods: {
    async onLoad () {
      if(!this.timestamp){
        this.timestamp= Date.now()
      }

      let res = await getArticleList({
        channel_id:this.channels[this.active].id,
        timestamp:this.timestamp
      })
      this.timestamp=res.data.data.pre_timestamp
      this.list.push(...res.data.data.results)
      console.log(res);


        // 加载状态结束
        this.loading = false
        // 数据全部加载完成
        if (this.list.length >= 50) {
          this.finished = true
        }
    },
    onRefresh () {
      this.timestamp = null
      this.finished = false
      this.list = []
      this.loading = true
      this.onLoad()
      this.isLoading = false
    },
    tabClick(){
      this.onRefresh()
    },
  },
}
</script>

<style lang="less">
.home-container {
  .van-nav-bar {
    z-index: 10;
    .van-nav-bar__content {
      background-color: #0098fa;
      .logo {
        width: 100px;
        height: 30px;
        background: url('~@/assets/logo.png');
        background-size: 100%;
        background-size: cover;
      }
      .van-button {
        height: 30px;
        background-color: #1dadfb;
        border: transparent;
        .van-icon {
          color: #fff;
          font-size: 18px;
        }
      }
    }
  }
  .van-tabs {
    .van-tabs__wrap {
      position: fixed;
      top: 46px;
      width: 100%;
      z-index: 10;
    }
    .van-tabs__content {
      margin-top: 46px;
      margin-bottom: 0;
    }
  }
}
</style>
